package com.sanpu.kxe.business.main.presenter;

import android.text.TextUtils;
import android.view.Gravity;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.HomeCenterOutput;
import com.sanpu.kxe.business.main.contract.AllApplicationContract;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.common.utils.ARouterUtils;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.rxservice.RequestMap;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：全部应用
 * Created by 薛勇军 on 2019/3/12.
 */

public class AllApplicationPresenter implements AllApplicationContract.Presenter {

    private AllApplicationContract.View mView;

    private CompositeSubscription mSubscription;

    public AllApplicationPresenter(AllApplicationContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void onItemClick(HomeCenterOutput output) {
        if (output != null) {
            switch (output.getSkipType()) {
                case MainConfig.SKIPTYPE_NATIVE:
                    String appNative = output.getAppNative().getAndroid();
                    if (ARouterUtils.containRouter(appNative)) {
                        ARouter.getInstance()
                                .build(appNative)
                                .withTransition(R.anim.in_from_right, R.anim.in_to_left)
                                .navigation();
                        ARouter.getInstance().build(appNative);
                    }
                    break;
                case MainConfig.SKIPTYPE_H5:
                    if (TextUtils.isEmpty(output.getUrl())) return;
                    MainOpenProvider.getInstance().openBrowserX5Activity(mView.getContext(),
                            UrlConstants.getBaseUrl() + output.getUrl() + "?" + new RequestMap().map2ReqStr(), true);
                    break;
                case MainConfig.SKIPTYPE_COMINGSOON:
                    new GlobalDialog(mView.getContext())
                            .title("即将上线")
                            .content("敬请期待！")
                            .gravity(Gravity.CENTER)
                            .show();
                    break;
            }
        }
    }

    @Override
    public void findHomeCenterMore() {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().findHomeCenterMore()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<HomeCenterOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onNext(List<HomeCenterOutput> centerOutputs) {
                        mView.showDataView(centerOutputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
